<?php
class Comments extends BlockInstance {
	public $CI;
	
	function Comments()
	{
		$this->CI =& get_instance();
	}
	
	function __call($m, $a)
	{
		global $BBX;
		
		$this->CI->load->helper('typography');
		$this->CI->load->helper('text');
		$this->CI->load->helper('url');
		$this->CI->load->helper('date');
		$this->CI->load->model('Content_model');
		
		if (preg_match('/[a-z]+_([a-z]+)/', $m, $matches))
		{
			if ($matches[1] == 'content')
			{
				$content = $a[1];
				$data 	 = array(
					'block_instance' => $a[0],
					'comments'		 => $this->_load_comments($content->id),
					'bricabox'		 => $BBX['bricabox'],	// pass anyway so handle_proxy() can recreate the view via ajax
					'content'		 => $content,			// pass anyway so handle_proxy() can recreate the view via ajax
					'inst'			 => $a[0]['data']
				);
				$this->CI->load->view('blocks/comments/content', $data);
			}
			elseif ($matches[1] == 'homepage')
			{
				$data = array(
					'bricabox'		 => $BBX['bricabox'],	// pass anyway so handle_proxy() can recreate the view via ajax
					'block_instance' => $a[0],
					'comments'		 => $this->_load_all_comments($a[1]),
				);
				$this->CI->load->view('blocks/comments/homepage', $data);
			}
		}
	}
	
	function _load_comments($content_id)
	{
		$query = $this->CI->db->query('select * from comments where content_id = ? order by created_at desc', array($content_id));
		if ($query->num_rows())
		{
			return $query;
		}
		else
			return false;
	}
	
	function _load_all_comments($all_content)
	{
		global $BBX;
		
		$query = $this->CI->db->query('select * from comments where content_id in
			(select id from contents where bricabox_id = ?)
			order by created_at desc', array($BBX['bricabox']->id));
		if ($query->num_rows())
		{
			return $query;
		}
		else
			return false;
	}
	
	/*
	public function handle_proxy($bricabox_id, $params, $parent_name, $tpl_path, $instance, $parent)
	{
		// lots of overhead to get us back into a renderable state...
		global $BBX;
		
		ob_start();
		
		$this->CI->load->model('Content_model');
		
		$block_data['data']			 = $instance;
		$block_data['parent']		 = $parent;
		$block_data['template_path'] = $tpl_path;
		$block_data['klass'] 		 = BlockInstance::load_block_file($tpl_path, $parent_name);
		$block_data['title'] 		 = BlockInstance::title_for_block($block_data);
		
		if (isset($params['content_id']))
			$content = $this->CI->Content_model->load_content($params['content_id'], $BBX['bricabox']->id);
		
		// actually handle the user action
		switch ($params['_action'])
		{
			case 'save_tags':
			{
				// steps to take:
				// 1. split tags by commas and strip out colons
				// 2. see if the tag exists; if so, get its id, otherwise create it and return the insert_id
				// 3. add a contents_tags record if one doesn't already exist
				$raw_tags = $params['value'];
				if (!strlen($raw_tags))
					break;
					
				foreach (explode(',', $raw_tags) as $tag)
				{
					$tag = preg_replace('/[^-_a-z0-9 ]/i', '', trim($tag)); // only allow alphanumerics, -, _, and whitespace
					$exists_query = $this->CI->db->query('select id from tags where name = ? limit 1', array($tag));
					if ($exists_query->num_rows() == 1)
						$tag_id = $exists_query->first_row()->id;
					else
					{
						// create new
						$new_query = $this->CI->db->query('insert into tags (created_at,updated_at,name)
							values(NOW(),NOW(),?)',
							array($tag));
						$tag_id = $this->CI->db->insert_id();
					}
					
					// join; does it exist?
					$exists_query = $this->CI->db->query('select * from contents_tags
						where tag_id = ? and content_id = ?
						limit 1',
						array($tag_id, $content->id));
					if ($exists_query->num_rows() == 0)
						$this->CI->db->query('insert into contents_tags (tag_id,content_id,user_id)
							values(?,?,?)',
							array(
								$tag_id,
								$content->id,
								$this->CI->userland->current_user()->id
							));
				}
			}
			break;
			
			case 'remove_tag':
			{
				$_params = array(
					$params['tag_id'],
					$params['content_id']
				);
				$this->CI->db->query('delete from contents_tags where tag_id = ? and content_id = ?', $_params);
			}
			break;
		}
		
		// expire cached files for any blocks rendered for this bricabox in the index view
		// $cache = BASEPATH."../cache/tags/{$m}_{$a[0]['data']->id}_bbx{$BBX['bricabox']->id}.html";
		// e.g. small_homepage_210_bbx15.html -> instance 210, bricabox_id = 15
		
		
		// load up the block again and return the generated html
		$this->either_content($block_data, $content);
		
		$_out = ob_get_contents();
		ob_end_clean();
		
		return $_out;
	}
	*/
}
?>